/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import { Space, Popconfirm, message } from 'antd';
import { ModalForm } from '@ant-design/pro-form';
import ProTable from '@ant-design/pro-table';
import DragTable from '@/components/DragTable';
import type { TableListItem } from '../../index.d';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import { getBrandAuthList, closeBrandCollect } from '@/services/yz/manage';
import styles from './index.less';

interface CollectionItem {
  brand: string;
  brand_cn: string;
  status: number;
  type: string;
}

interface KbCollectionModal {
  item: TableListItem;
  children: JSX.Element;
}

const KbOptions = ({
  record,
  item,
  actionRef,
}: {
  record: CollectionItem;
  item: TableListItem;
  actionRef: any;
}) => {
  const { status, brand_cn, brand } = record;
  const { dak_id: cm_id, phone } = item;
  const optionList = [
    {
      status: '已禁用',
      option: '开启',
      is_close: 3,
      title: '开启后，该驿站可重新开通该品牌驿站代收，是否继续',
      tips: '已开启',
    },
    {
      status: '状态正常',
      option: '禁用',
      is_close: 2,
      title: `您将关闭该驿站${brand_cn}品牌驿站代收功能，且驿站不能重新开通，是否继续？`,
      tips: `已禁用（${brand_cn}）驿站代收功能`,
    },
    {
      status: '已驳回',
      option: '清空信息',
      is_close: 1,
      title: '确认后将清空圆通驿站代收信息，请谨慎使用！',
      tips: '已清空',
    },
    {
      status: '已开通仍驳回',
      option: '',
    },
  ];
  const recordItem = optionList[status];

  const handleOption = (is_close?: number) => {
    closeBrandCollect({ cm_id, phone, brand, is_close }).then((res) => {
      const { code, msg } = res;
      if (code === 0) {
        actionRef.current?.reload();
        message.success(msg);
      } else {
        message.error(msg);
      }
    });
  };

  return (
    <Space direction='vertical'>
      {recordItem.status ? (
        <span style={{ color: status === 3 ? 'red' : '' }}>{recordItem.status}</span>
      ) : (
        <span>未知状态</span>
      )}
      {recordItem.option ? (
        <Popconfirm
          title={recordItem.title}
          onConfirm={handleOption.bind(null, recordItem.is_close)}
        >
          <a>{recordItem.option}</a>
        </Popconfirm>
      ) : null}
    </Space>
  );
};

const Index = ({ children, item }: KbCollectionModal) => {
  const actionRef = useRef<ActionType>();
  const queryTable = async () => {
    const { id, dak_id: cm_id } = item;
    const { code, data, msg } = await getBrandAuthList({ id, cm_id });
    if (code == 0) {
      return {
        data: data.map((ite: CollectionItem, index: number) => ({ ...ite, id: index })),
      };
    } else {
      message.error(msg);
      return {};
    }
  };

  const columns: ProColumns<CollectionItem>[] = [
    {
      title: '品牌',
      dataIndex: 'brand_cn',
      align: 'center',
    },
    {
      title: '代收类别',
      dataIndex: 'type',
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'options',
      align: 'center',
      render: (_, record) => {
        return <KbOptions record={record} item={item} actionRef={actionRef}></KbOptions>;
      },
    },
  ];

  return (
    <ModalForm<{
      name: string;
      company: string;
    }>
      title='驿站代收管理'
      trigger={children}
      submitter={false}
      modalProps={{
        bodyStyle: { padding: 0, height: 500 },
        centered: true,
      }}
    >
      <DragTable>
        <ProTable
          actionRef={actionRef}
          rowKey='id'
          className={styles.collectionModal}
          headerTitle={item.dak_name + '-' + item.phone}
          request={queryTable}
          columns={columns}
          search={false}
          pagination={false}
          scroll={{ x: 640 }}
        />
      </DragTable>
    </ModalForm>
  );
};

export default Index;
